﻿using System;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Avalonia.Layout;
using WeatherCore.Lib;

namespace WeatherCore.App.Converter;

public class ProgressDataTemplate : IDataTemplate
{
    public Control Build(object? param)
    {
        if (param is not WeatherToday today) return new TextBlock();
        var day = DateProgress(today.sunrise, today.sunset);
        if (day == 100)
        {
            day = DateProgress(today.moonrise, today.moonSet);
            if (day == 100)
                return new TextBlock()
                {
                    Text = "今夜乌黑 , 竟也找寻不到任何月光的痕迹", FontSize = 18, HorizontalAlignment = HorizontalAlignment.Center
                };

            var sunriseText = new TextBlock()
            {
                Text = $"月出时间 {today.moonrise}", HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };
            var sunsetText = new TextBlock()
            {
                Text = $"月落时间 {today.moonSet}", HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };
            var icon = new Avalonia.Svg.Skia.Svg(
                new Uri("avares://WeatherCore.App/Assets/"))
            {
                Path = $"/Assets/WeatherIcon/{today.moonPhaseIcon}.svg", Height = 50
            };
            icon.Classes.Add("Yellow");
            var grid = new Grid()
            {
                Children = { sunriseText, icon, sunsetText },
                ColumnDefinitions = ColumnDefinitions.Parse("*,*,*")
            };
            ToolTip.SetTip(grid, today.moonPhase);
            Grid.SetColumn(sunriseText, 0);
            Grid.SetColumn(icon, 1);
            Grid.SetColumn(sunsetText, 2);
            var panel = new StackPanel() { Children = { grid, new ProgressBar() { Value = day } } };
            return panel;
        }
        else
        {
            var sunriseText = new TextBlock()
            {
                Text = $"日出时间 {today.sunrise}", HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };
            var sunsetText = new TextBlock()
            {
                Text = $"日落时间 {today.sunset}", HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };
            var icon = new Avalonia.Svg.Skia.Svg(
                new Uri("avares://WeatherCore.App/Assets/"))
            {
                Path = $"/Assets/WeatherIcon/{today.iconDay}.svg", Height = 50,
            };
            icon.Classes.Add("Blue");
            var grid = new Grid()
            {
                Children = { sunriseText, icon, sunsetText },
                ColumnDefinitions = ColumnDefinitions.Parse("*,*,*")
            };
            ToolTip.SetTip(grid, today.textDay);
            Grid.SetColumn(sunriseText, 0);
            Grid.SetColumn(icon, 1);
            Grid.SetColumn(sunsetText, 2);
            var panel = new StackPanel() { Children = { grid, new ProgressBar() { Value = day } } };
            return panel;
        }
    }

    private int DateProgress(string start, string end)
    {
        if (string.IsNullOrEmpty(start) || string.IsNullOrEmpty(end)) return 100;

        var now = DateTime.Now;
        var startDate = DateTime.Parse(start);
        var endDate = DateTime.Parse(end);
        var total = endDate - startDate;
        var nowProgress = now - startDate;
        var totalMin = total.TotalMinutes;
        if (totalMin < 0)
            totalMin = 24 * 60 - totalMin;
        var result = (int)(nowProgress.TotalMinutes / totalMin * 100);
        return result > 100 ? 100 : result;
    }

    public bool Match(object? data) => data is WeatherToday;
}

public class WarningDataTemplate : IDataTemplate
{
    private Dictionary<string, string> ColorChinese => new()
    {
        { "White", "白色" },
        { "Blue", "蓝色" },
        { "Green", "绿色" },
        { "Yellow", "黄色" },
        { "Orange", "橙色" },
        { "Red", "红色" },
        { "Black", "黑色" }
    };

    public Control? Build(object? param)
    {
        if (param is not WeatherWarning warning) return null;
        var border = new Border();
        border.Classes.Add("Card");
        var icon = new Avalonia.Svg.Skia.Svg(
            new Uri("avares://WeatherCore.App/Assets/"))
        {
            Path = $"/Assets/WeatherIcon/{warning.type}.svg", Height = 25
        };
        if (!string.IsNullOrEmpty(warning.severityColor))
            icon.Classes.AddRange([warning.severityColor]);

        var text = new TextBlock()
        {
            Text =
                $"{warning.typeName}" +
                $"{(string.IsNullOrEmpty(warning.severityColor) ? "" : ColorChinese[warning.severityColor])}" +
                $"预警",
            Margin = new Thickness(5),
            HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center
        };
        var panel = new WrapPanel()
        {
            Children = { icon, text }
        };
        border.Child = panel;
        return border;
    }

    public bool Match(object? data) => data is WeatherWarning;
}